package com.china08.yxyapi.api.manage.stu.myclass.moments;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.mg.moments.YxyMoments;
import com.china08.yxyapi.entity.mg.moments.YxyMomentsPraise;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsPraiseRepository;
import com.china08.yxyapi.repository.mg.moments.YxyMomentsRepository;
import com.china08.yxyapi.security.profile.YxyProfile;
import com.china08.yxyapi.util.YxUserPointsUtilV3;

/**
 * 班级圈--家长端--点赞
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class MomentsStuPraiseController extends YxyApiBaseController {
    @Autowired
    private YxyMomentsPraiseRepository yxyMomentsPraiseRepository;

    @Autowired
    private YxyMomentsRepository yxyMomentsRepository;

    @Autowired
    private YxUserPointsUtilV3 yxUserPointsUtilV3;

    @RequestMapping(value = "/api_yw/manage/stu/moments/StuPraise", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public RespModel precess(HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody(required = false) ReqModel reqModel,
            RespModel respModel) {

        YxyProfile profile = getProfile(request, response);
        String userId = profile.getId();
        String userNick = profile.getUserNick();
        String momentsId = reqModel.momentsId;
        YxyMomentsPraise yxyMomentsPraise = yxyMomentsPraiseRepository
                .findByMomentsIdAndUserId(momentsId, userId);
        // 操作成功返回消息
        String message = "";
        if (yxyMomentsPraise == null) {// 点赞
            yxyMomentsPraise = new YxyMomentsPraise();
            yxyMomentsPraise.setUserId(userId);
            yxyMomentsPraise.setUserNick(userNick);
            yxyMomentsPraise.setMomentsId(momentsId);
            yxyMomentsPraiseRepository.save(yxyMomentsPraise);

            yxUserPointsUtilV3.praiseClassMessage(userId);
            message = "点赞成功";
        } else {// 取消赞
            yxyMomentsPraiseRepository.delete(yxyMomentsPraise.getId());
            message = "取消点赞成功";
        }
        Integer praiseCount = yxyMomentsPraiseRepository
                .countByMomentsId(momentsId);
        YxyMoments yxyMoments = yxyMomentsRepository
                .findById(reqModel.momentsId);
        Assert.notNull(yxyMoments, "班级圈已不存在");
        yxyMoments.setPraiseCount(praiseCount);
        yxyMomentsRepository.save(yxyMoments);
        respModel.setMessage(message);
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {
        // 对应的班级圈id
        public String momentsId;

    }

    protected static class RespModel extends YxyApiRespModel {
    }
}
